package com.it.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.it.gmall.common.result.Result;
import com.it.gmall.model.product.*;
import com.it.gmall.product.service.ManageService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
//@CrossOrigin
public class ManageController {

    @Autowired
    private ManageService manageService;


    /**
     * 查看一级分类
     * @return
     */
    @GetMapping("/getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> category1List = manageService.getCategory1();
        return Result.ok(category1List);
    }

    /**
     * 查看二级分类
     * @param category1Id 一级分类的id
     * @return
     */
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable long category1Id){
        List<BaseCategory2> category2List = manageService.getCategory2(category1Id);
        return Result.ok(category2List);
    }

    /**
     * 查看三级分类
     * @param category2Id 二级分类的id
     * @return
     */
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable long category2Id){
        List<BaseCategory3> category3List = manageService.getCategory3(category2Id);
        return Result.ok(category3List);
    }

    /**
     * 根据分类id获取平台属性
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable long category1Id,
            @PathVariable long category2Id,
            @PathVariable long category3Id
    ){
        List<BaseAttrInfo> list = manageService.attrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(list);
    }

    /**
     * 添加属性信息
     * @return
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    /**
     * 根据id获取属性对应的属性值
     * @return
     */
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable long attrId){
        List<BaseAttrValue> list = manageService.getAttrValueList(attrId);
        return Result.ok(list);
    }

    /**
     * 分页查询商品品牌列表
     * @param page 页码
     * @param limit 条数
     * @return
     */
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable Integer page,@PathVariable Integer limit){
        IPage<BaseTrademark> iPage = manageService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }

    /**
     * 添加品牌信息
     * @param baseTrademark 品牌数据
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result save(@RequestBody BaseTrademark baseTrademark){
            manageService.save(baseTrademark);
        return Result.ok();
    }

    /**
     * 修改 品牌信息
     * @param baseTrademark 品牌数据
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result update(@RequestBody BaseTrademark baseTrademark){
        manageService.update(baseTrademark);
        return Result.ok();
    }

    /**
     * 根据id获取品牌数据
     * @param tradeMarkId
     * @return
     */
    @GetMapping("/baseTrademark/get/{tradeMarkId}")
    public Result get(@PathVariable long tradeMarkId){
        BaseTrademark baseTrademark = manageService.get(tradeMarkId);
        return Result.ok(baseTrademark);
    }

    /**
     * 根据id删除品牌数据
     * @param tradeMarkId
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{tradeMarkId}")
    public Result remove(@PathVariable long tradeMarkId){
        manageService.remove(tradeMarkId);
        return Result.ok();
    }

    /**
     * 获取spu列表
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
    @GetMapping("{page}/{limit}")
    public Result spuList(@PathVariable Integer page,@PathVariable Integer limit,long category3Id){
        IPage<SpuInfo> list = manageService.spuList(page,limit,category3Id);
        return Result.ok(list);
    }

    /**
     * 获取品牌列表
     * @return
     */
    @GetMapping("baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademark = manageService.getTrademarkList();
        return Result.ok(baseTrademark);
    }

    /**
     * 获取销售属性列表
     * @return
     */
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    /**
     * 添加spu
     * @param spuInfo
     * @return
     */
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 获取spu的所有图片
     * @param spuId
     * @return
     */
    @GetMapping("spuImageList/{spuId}")
    public Result spuImageList(@PathVariable long spuId){
        List<SpuImage> spuImage = manageService.spuImageList(spuId);
        return Result.ok(spuImage);
    }

    /**
     * 获取spu的所有销售属性
     * @param spuId
     * @return
     */
    @GetMapping("spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable long spuId){
        List<SpuSaleAttr> spuSaleAttrList= manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    /**
     * 保存商品的sku信息
     * @param skuInfo
     * @return
     */
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 获取sku列表
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list/{page}/{limit}")
    public Result skuList(@PathVariable Integer page,@PathVariable Integer limit){
        IPage<SkuInfo> skuInfoIPage = manageService.skuList(page,limit);
        return Result.ok(skuInfoIPage);
    }

    /**
     * 上架sku商品
     * @param skuId
     * @return
     */
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Integer skuId){
        manageService.onSale(skuId);

        // todo 放到es中 考虑事务的问题

        return Result.ok();
    }

    /**
     * 下架sku商品
     * @param skuId
     * @return
     */
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Integer skuId){
        manageService.cancelSale(skuId);

        // todo 从es中删除 考虑事务的问题

        return Result.ok();
    }
}
